using System;
using System.Globalization;
using Medianamik.Core;
using Medianamik.Core.Configuration;
using Medianamik.Core.Urls;
using Medianamik.Tests.Unit.Fakes;
using NUnit.Framework;

namespace Medianamik.Tests.Unit.Medianamik.Core.Urls
{
    [TestFixture]
    public class PathBuilderTest
    {
        private IUrlDictionary _urlDictionay;
        private WebSiteFake _webSite;
        private static readonly MedianamikConfigSection MedianamikConfig = new MedianamikConfigSection();

        [SetUp]
        public void Init()
        {
            // _pathBuilder = new PathBuilder(new FakeUrlDictionary(), new PathSegmentFormater());

            MedianamikConfig.DefaultPageExtension = ".html";

            _urlDictionay = new FakeUrlDictionary();
            _urlDictionay.Initialize(MedianamikConfig);

            _webSite = new WebSiteFake(Guid.NewGuid(), String.Empty);
        }

        [Test]
        public void PathSegments_should_be_separated_by_slashes()
        {
            var segments = new PathSegmentCollection();
            segments.Add(0, new PathSegment("test1"));
            segments.Add(1, new PathSegment("test2"));

            var expected = "/test1/test2";
            var actual = _urlDictionay.BuildUniqueRelativeUrlWithoutPrefixAndExtension(segments, new CultureInfo("fr-CA"), _webSite);

            Assert.That(actual, Is.EqualTo(expected));
        }

        [Test]
        public void Generated_path_should_start_with_slash()
        {
            var segments = new PathSegmentCollection();
            segments.Add(0, new PathSegment("test"));

            var expected = "/test";
            var actual = _urlDictionay.BuildUniqueRelativeUrlWithoutPrefixAndExtension(segments, new CultureInfo("fr-CA"), _webSite);

            Assert.That(actual, Is.EqualTo(expected));
        }

        [Test]
        public void Prefix_should_not_be_appended_to_generated_path()
        {
            var segments = new PathSegmentCollection();
            segments.Add(0, new PathSegment("test"));
            var prefix = "prefix";

            var expected = "/test";
            var actual = _urlDictionay.BuildUniqueRelativeUrlWithoutPrefixAndExtension(segments, new CultureInfo("fr-CA"), _webSite);

            Assert.That(actual, Is.EqualTo(expected));
        }

        [Test]
        public void DefaultExtension_should_not_be_appended_to_generated_path()
        {
            var segments = new PathSegmentCollection();
            segments.Add(0, new PathSegment("test"));
            MedianamikConfig.DefaultPageExtension = ".extension";

            var expected = "/test";
            var actual = _urlDictionay.BuildUniqueRelativeUrlWithoutPrefixAndExtension(segments, new CultureInfo("fr-CA"), _webSite);

            Assert.That(actual, Is.EqualTo(expected));
        }
    }

    public class WebSiteFake : WebSite
    {
        private string _prefix;

        public WebSiteFake(Guid nodeId, string prefix)
        {
            var innerNode = new Node(nodeId, MedianamikTypes.WebSite, null, null, null, false);
            innerNode.NamePropertyId = Guid.NewGuid();
            InnerNode = innerNode;
            _prefix = prefix;
        }

        public override string Prefix
        {
            get { return _prefix; }
        }

    }

    public class PathBuilderFake
    {


    }
}